package com.practice.pagehelper.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @Description :
 * @Version : V1.0.0
 * @Date : 2022/9/6 20:50
 */
@Component
@Aspect
public class WebAspect {

    @Pointcut("execution(* com.practice.pagehelper.aop..*.*(..))")
    public void execution1() {
    }

    @Before("execution1()")
    public void beforeAdvice(JoinPoint joinPoint) {
        System.out.println("- - - - - 前置通知 - - - - -");
        Signature signature = joinPoint.getSignature();
        System.out.println("返回目标方法的签名："+signature);
        System.out.println("代理的是哪个方法:" + signature.getName());
        System.out.println("获取目标方法的参数信息:" + Arrays.asList(joinPoint.getArgs()));
    }

    @After("execution1()")
    public void afterAdvice() {
        System.out.println("后置通知");
    }

    @AfterReturning(value = "execution1()", returning = "values")
    public void afterReturnAdvice(JoinPoint joinPoint, String values) {
        System.out.println("后置返回通知");
        System.out.println("后置返回通知, 返回值:" + values);
    }


    @AfterThrowing(value = "execution1()", throwing = "exception")
    public void afterThrowingAdvice(JoinPoint joinPoint, NullPointerException exception) {
        System.out.println("后置异常通知");
    }

    @Around(value = "execution1()")
    public Object aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) {
        System.out.println("环绕通知");
        System.out.println("环绕通知方法名:" + proceedingJoinPoint.getSignature().getName());

        try {
            Object proceed = proceedingJoinPoint.proceed();
            return proceed;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }
}
